class ChromePluginPdd extends ChromePlugin {
    constructor() {
      super();
      this.exportOrderList = this.exportOrderList.bind(this);
    }
    install() {
       console.log("初始化拼多多插件")
       // 安装拼多多 
       this.initUI()
    }
    initUI() {
        let $floatingButton = $('<div class="ey-floating-button">批量导出</div>');
        $("body").append($floatingButton);
        // 悬浮按钮点击事件
        $floatingButton.click(this.exportOrderList);
        
    }
    // 解析订单列表--获取数据
    parserAjaxOrderList(page = 1, offset = 0) {
      return new Promise((resolve, reject) => {
        $.ajax({
          url: 'https://yangkeduo.com/proxy/api/api/aristotle/order_list_v4?pdduid=2222194961787&is_back=1',
          crossDomain: true,
          method: 'post',
          contentType: 'application/json;charset=UTF-8',
          data: JSON.stringify({
            'type': 'all',
            'page': page,
            'origin_host_name': 'yangkeduo.com',
            'page_from': 0,
            'anti_content': '0asAfqnyUiPssgE9Q2iNxe5Z9Lu4SPk4gtVe-uUeMkJ8UK7yyj7FjxI8NHqcoO8uJpRlpcrYM__gCHI4qtwH291POSLu9C0yNf0e9HfUMwEpACTCO84UfwLlxfjLn9rhnorhhmiAQNPb6G2fMPtJ6pWz_2uHYOMZ2Wa8xYK-ZBAamsJvwo0mmA6Hlwb0y3VKb-rHlxbBqb35HgjxjOaaLUDCvWGvEWVlQEvXT_WlsL9Gxjog-7HkRP6d7smfiEmhxlD8Bjhak_rof3yGE9fuazr20DVBhEYzPED84iCBFLDfj8Ib9gmuxqBfzYnGG5ZGcBYYPsjlPzu2qca5QyPJH9tOCftN4h8gxZ2-iCiSR6v0RwqA6Twu8lGKBN93gxpYxo8ABxwYWkZugB5d3IZyEayXhfyYFpUbLkOSa2Y9tmvKfSfdyaB8B1dFs_d84Ig4dzdj0RwCpGP_lVZsZvdddg26kmjvepoMK0_9eeV7XJIyJyMMHGr8jIJjIlHMRQNQS3V2PEpWPuEGNhrlme7YJHsf5GTB0OjD64WkgO8MK91jZdTjH3GHZxLAf1Vk1vPpFQwg_SELkk8xFwMsNdt4EB_Kv3gVmvdnwBISxKy7deMdDfnX5y4aiXHYWfdDvOInnxRmtc82kvLOpI10nL8KtUAv19st1aFfwF3zGLe37xdF3Cw0TUh38V8S0k-tLt12RyE6rsvQe8XwwniWugmtMekwm7Wyi7kme1YA4njRDNIDRYoiNhNhUMMDme8jllBGNyhHsLWu8fJdX1WL8-fM8pNC-VUBrcnfCHje9l2wY7QaWzVrDHcpVrnZZVAqPpZFFxP1WsTl-wjeVwGXv_gBcvZwq3WdP18ewhiy7eGYh_5d4bew4ZdrUCKV7MNHjEPbVmer4haP3VM09o',
            'size': 10,
            'offset': offset
          })
        }).done(({ orders }) => {
          console.log("当前第" + page + "页")
          let loading = document.querySelector('.loading-icon')
          loading.innerText = `第${page}页`
          if (orders.length === 0) {
              page = -1
          }
          orders.forEach(element => {
            // 订单状态
            const order_status = element['order_status']
            // 下单时间
            const order_time = formatDate(element['order_time'])
            // 偏移
            offset = element['offset']
            // 订单编号
            const order_number = element['order_sn']
            // 运费
            const shipping_amount = element['shipping_amount'] / 100
            // 付款金额
            const order_amount = element['order_amount'] / 100
            if (order_status != 1 || order_status != 2) {
                // 评价待收货的数据才要
                this.orderList.push([order_time, 
                  order_number, 
                  order_amount, 
                  shipping_amount, 
                  (order_status == 0 ? "待评价" : "待收货")])
            }
            
          });
          if (page !== -1) {
            this.parserAjaxOrderList(++page, offset)
          } else {
            hideLoadingMask()
            this.outputExcel()
          }
        })
      })
    }
    /**
     * page 页数
     * offset 偏移量
     * firstPageLastOffset 
     * 导出时间处理函数
     */
    exportOrderList() {
        // 装产品的容器
        this.orderList = []
        // 初始化的offset
        let offsetList =  Object.values(window.rawData.ordersStore.ordersService.offset)
        for (const item of offsetList) {
         if (item !== 0) {
           this.startOffset = item
           break
         }
        }
        // 加载模态框
        showLoadingMask()
        // 批量获取数据
        this.parserAjaxOrderList(1, this.startOffset)
        
    } 
    /**
     * 输出excel
     */
    outputExcel() {
      exportToCSV(
        ["下单时间", "订单号", "付款金额", "运费", "订单状态"],
        this.orderList,
        "拼多多订单数据"
      );
      alert("导出成功!!!!!")
    }
    
}

// 240428-238362315543451  1评价  240428-238362315543451 收货 240506-330076025623451 2收货